<template>
  <div>
    <div class="title">
      正在修改<span @click="back" class="back">返回</span>
    </div>
    <div class="icon" v-for="item of list" :key="item.id">
      <div class="icon-img">
        <img :src="imgBaseURL + item.imgUrl" />
        <div class="del" @click="del(item)">
          <img src="@/assets/images/icons/remove.png" />
        </div>
      </div>
    </div>
    <div class="icon">
      <img
        @click="$refs.addPicFile.click()"
        src="@/assets/images/icons/添加图片.png"
        mode="widthFix"
        class="icon-img"
      />
    </div>
    <input
      type="file"
      @change="addPic"
      multiple
      accept="image/jpg,image/jpeg,image/png,image/bmp"
      style="display: none"
      ref="addPicFile"
    />
  </div>
</template>
<script>
/* eslint-disable */
import axios from "axios";
export default {
  name: "SwiperList",
  props: {
    list: Array,
  },
  data() {
    return {
      imgBaseURL: process.env.BASE_API,
    };
  },
  methods: {
    del: function (item) {
      console.log(item);
      axios.defaults.baseURL = process.env.BASE_API;
      axios
        .get("/api/swiper/del/" + item.id)
        .then(this.list.splice(this.list.indexOf(item), 1));
    },
    addPic: function (e) {
      var that = this;
      let formData = new window.FormData();
      for (let i = 0; i < e.target.files.length; i++) {
        formData.append("file", e.target.files[i]);
        let ext = e.target.files[i].name.split(".")[1];
        if (!(ext === "jpg" || ext === "png" || ext === "jpeg")) {
          console.log("格式错误");
          return;
        }
        axios.post("/api/swiper/add", formData).then(this.uploadSucc);
      }
    },
    uploadSucc: function () {
      console.log("显示图片");
      this.$parent.getSwiper();
    },
    back: function () {
      this.$router.push("/");
    },
  },
};
</script>
<style scoped>
.title {
  margin-top: 0.2rem;
  line-height: 0.8rem;
  background: #eee;
  text-indent: 0.2rem;
}

.back {
  color: #ff9300;
  float: right;
  margin-right: 0.16rem;
}
.icon {
  position: relative;
  overflow: hidden;
  width: calc(25% - 2px);
  float: left;
  height: 0;
  padding-bottom: 25%;
  border: solid 1px #f5f5f5;
}

.icon-img {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  /* bottom: 0.44rem; */
  /* box-sizing: border-box; */
  width: 90%;
  height: 90%;
  margin-left: auto;
  margin-right: auto;
  transform: translateY(-50%);
  object-fit: cover;
}

.icon-img img {
  display: block;
  height: 100%;
  margin: 0 auto;
}

.del {
  position: absolute;
  top: 1px;
  right: 1px;
  z-index: 99;
}
</style>
